package com.nblog.config;
import java.io.File;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.ext.handler.ContextPathHandler;
import com.jfinal.ext.interceptor.SessionInViewInterceptor;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.ehcache.EhCachePlugin;
import com.jfinal.template.Engine;
import com.nblog.annotation.AutoBindRoutes;
import com.nblog.interceptor.admin.SysLogInterceptor;
import com.nblog.model._MappingKit;
import com.nblog.plugin.ioc.IocPlugin;
import com.nblog.plugin.ioc.core.IocControllerFactory;
import com.nblog.tag.Button;

public class Config extends JFinalConfig {

	@Override
	public void configConstant(Constants me) {
		me.setEncoding("utf-8");
		PropKit.use("config.properties");
		me.setDevMode(PropKit.getBoolean("devMode", false));
		me.setControllerFactory(new IocControllerFactory());
		me.setBaseUploadPath(PathKit.getWebRootPath()+File.separator+"uploads");
	}

	@Override
	public void configEngine(Engine me) {
		me.addDirective("button",Button.class);
	}

	@Override
	public void configHandler(Handlers me) {
		me.add(new ContextPathHandler("ctx"));
	}

	@Override
	public void configInterceptor(Interceptors me) {
		me.add(new SessionInViewInterceptor());
		//me.addGlobalActionInterceptor(new SysLogInterceptor());
	}
	public static DruidPlugin createDruidPlugin() {
		if(PropKit.getBoolean("devMode", false)){
			return new DruidPlugin(PropKit.get("dev.jdbcUrl"), PropKit.get("dev.user"), PropKit.get("dev.password").trim());
		}else{
			return new DruidPlugin(PropKit.get("pro.jdbcUrl"), PropKit.get("pro.user"), PropKit.get("pro.password").trim());
		}
		
	}
	@Override
	public void configPlugin(Plugins me) {
		DruidPlugin druidPlugin = createDruidPlugin();
		me.add(druidPlugin);
		ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
		arp.setShowSql(true);
		arp.setDevMode(true);
		_MappingKit.mapping(arp);
		me.add(arp);
		me.add(new EhCachePlugin());
		//Cron4jPlugin cp = new Cron4jPlugin("cron4j.properties", "cron4j");
		//me.add(cp);
		//ioc依賴注入
		String basePackage = "com.nblog";
		me.add(new IocPlugin(basePackage));
	}

	@Override
	public void configRoute(Routes me) {
		AutoBindRoutes route=new AutoBindRoutes();
		route.setBaseViewPath("/WEB-INF/views");
		me.add(route);
	}

}
